package file;


import java.io.*;
import java.util.Scanner;

public class demo11 {
    public static void main(String[] args)throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的文件路径：");
        String src = scanner.next();
        File srcFile = new File(src);
        if(!srcFile.isFile()){
            System.out.println("输入的源文件路径非法！");
            return;
        }
        System.out.println("请输入要复制到的目标路径：");
        String dest = scanner.next();
        File destFile = new File(dest);
        //不要求目标文件本身存在，但是要保证目标文件所在的目录得是存在的
        //假设目标文件写作d:/tmp/cat2.jpg , 就需要保证d:/tmp目录是存在的
        if(!destFile.getParentFile().isDirectory()){
            System.out.println("输入的目标文件路径不合法！");
            return;
        }
        //进行复制操作的过程，按照字节流打开
        try(InputStream inputStream = new FileInputStream(srcFile);
            OutputStream outputStream = new FileOutputStream(destFile)
        ){
            while (true){
                byte[]buffer = new byte[20480];
                int n = inputStream.read(buffer);
                System.out.println("n="+n);
                if(n==-1){
                    System.out.println("读取到eof，循环结束。");
                    break;
                }
                outputStream.write(buffer,0,n);
            }

        }




    }
}
